package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: gpereir
 * Date: 5/21/13
 * Time: 2:55 PM
 * To change this template use File | Settings | File Templates.
 */
public class BibliotecaApp {
    private IOHandler io;
    private String userInput = "", login = "", password = "";
    private Boolean userIsLogged = false;
    private Integer userId = 0;
    private Database database;
    private Menu menu;

    public BibliotecaApp(IOHandler ioHandler, Database database) {
        this.database = database;
        this.io = ioHandler;
        menu = new Menu(io);
    }

    public void start() {
        database.Bootstrap();

        showWelcomeMessage();

        while (!userInput.equals("0")) {
            menu.showMenu();
            userInput = this.io.getInput();
            selectOption(Integer.parseInt(userInput));
        }

    }


    public void showWelcomeMessage() {
        io.printMessage("Welcome, customer!\n");
    }

    public void selectOption(int option) {
        switch (option) {
            case 1:
                logIn();
                break;
            case 2:
                listAllBooks();
                break;
            case 3:
                if (userIsLogged) {
                    reserveBook();
                } else {
                    logIn();
                }
                break;
            case 4:
                listAllMovies();
                break;
            case 5:
                if (userIsLogged) {
                    for (UserList u : this.database.getUsers()) {
                        if (u.getIdUser() == userId) {
                            io.printMessage("NAME: " + u.getName() + ", E-MAIL: " + u.getEmail() + ", PHONE: " + u.getPhone() + "\n");
                        }
                    }
                }
                else
                {
                    io.printMessage("Please talk to a Librarian. Thank you.\n");
                }
                break;
            case 0:
                return;
            default:
                io.printMessage("Select a valid option!\n");

        }
    }

    private void logIn() {
        io.printMessage("Please inform your username: ");
        login = this.io.getInput();
        for (UserList u : this.database.getUsers()) {
            if (u.getLogin().equals(login)) {
                io.printMessage("Inform password: ");
                password = this.io.getInput();
                if (u.getPassword().equals(password)) {
                    userId = u.getIdUser();
                    userIsLogged = true;
                    io.printMessage("You're logged in now!\n");
                } else {
                    userIsLogged = false;
                    io.printMessage("Wrong password!\n");
                }
            }
        }
    }

    public void showReserveABookMessage() {
        io.printMessage("Please inform the ID of the book: ");
    }

    public void listAllBooks() {
        for (BookList b : this.database.getBooks()) {
            io.printMessage(b.getIdBook() + " - " + b.getName() + " by " + b.getAuhor() + "\n");
        }
    }

    public void reserveBook() {
        showReserveABookMessage();
        userInput = this.io.getInput();
        int idBook = Integer.parseInt(userInput);

        for (BookList b : this.database.getBooks()) {
            if (b.getIdBook() == idBook) {
                //Verify if book is available before reserving
                if (b.getAvailable()) {
                    b.setAvailable(false);
                    showSuccessfulReserve();
                } else {
                    showUnsuccessfulReserve();
                }
            }

        }
    }


    public void showSuccessfulReserve() {
        io.printMessage("*** Thank You! Enjoy the book ***\n");
    }

    public void showUnsuccessfulReserve() {
        io.printMessage("*** Sorry, we don't have that book yet ***\n");
    }

    public void listAllMovies() {
        String newLineMark = System.getProperty("line.separator");
        String leftAlignFormat = "| %-23s | %-4d | %-17s | %-6s |" + newLineMark;

        printHeader(newLineMark);
        for (MovieList m : this.database.getMovies()) {
            printMovieDetails(leftAlignFormat, m);
        }
    }

    private void printMovieDetails(String leftAlignFormat, MovieList m) {
        io.printMessage(String.format(leftAlignFormat, m.getName(), m.getYear(), m.getDirector(), (m.getRating() == null) ? "N/A" : m.getRating()));
    }

    private void printHeader(String newLineMark) {
        String header = "+-------------------------+------+-------------------+--------+" + newLineMark +
                "| Movie                   | Year | Director          | Rating |" + newLineMark +
                "+-------------------------+------+-------------------+--------+" + newLineMark;

        io.printMessage(header);
    }
}
